package astra.client.pages;

import astra.client.Astra;
import astra.client.models.Employee;
import astra.client.pages.clients.ClientsPage;
import astra.client.pages.employee.EmployeePage;
import astra.client.pages.purchase.PurchasePage;
import astra.client.pages.supplier.SupplierPage;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Kotelnikov Dmitry <a href="mailto:dimonnot@mail.ru"/>
 */
public class MainLayout extends Page{
    interface MainLayoutUiBinder extends UiBinder<HTMLPanel, MainLayout> {
    }

    private static MainLayoutUiBinder ourUiBinder = GWT.create(MainLayoutUiBinder.class);

    @UiField
    TabLayoutPanel tabPanel;

    @UiField
    Label groupName;

    @UiField
    Hyperlink employeeName;

    @UiField
    ClientsPage clientsPage;

    @UiField
    EmployeePage employeePage;

    @UiField
    SupplierPage supplierPage;

    @UiField
    PurchasePage purchasePage;

    protected Map<Widget, Page> pages = new HashMap<Widget, Page>();

    public MainLayout(Astra astra) {
        super("");

        initWidget(ourUiBinder.createAndBindUi(this));

        purchasePage.show();
        pages.put(purchasePage.asWidget(), purchasePage);
        pages.put(clientsPage.asWidget(), clientsPage);
        pages.put(employeePage.asWidget(), employeePage);
        pages.put(supplierPage.asWidget(), supplierPage);

        tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
                pages.get(
                    tabPanel.getWidget(integerSelectionEvent.getSelectedItem())
                ).show();
            }
        });

        /*
         * Clear stupid GWT inline styles:
         * position:absolute
         * overflow:hidden
         */
        Element element = tabPanel.getElement();
        clearStyles(element, 3);
        element.getFirstChild().removeFromParent();
    }

    protected void clearStyles(Element element, int depth) {
        NodeList<Node> nodeList = element.getChildNodes();
        int size = element.getChildCount();
        if (element.getClassName() != null) {
            if (element.getClassName().contains("gwt-TabLayoutPanelContentContainer")){
                nodeList.getItem(0).removeFromParent();
            }
        }
        for (int i = 0; i < size; i++) {
            Element subElement = (Element)nodeList.getItem(i);
            Style style = subElement.getStyle();
            style.clearPosition();
            style.clearOverflow();
            if (depth > 0) {
                clearStyles(subElement, depth - 1);
            }
        }
    }

    public void show(Employee employee) {
        groupName.setText(employee.getGroupName() + ": ");
        employeeName.setText(employee.getName());

        for (Map.Entry<Widget, Page> entry:pages.entrySet()) {
            Page page = entry.getValue();
            Employee.Access access = employee.getAccess(page.getName());

            int index = tabPanel.getWidgetIndex(entry.getKey());
            if (access == Employee.Access.DENIED) {
                tabPanel.getTabWidget(index).addStyleName("hidden");
            } else {
                tabPanel.getTabWidget(index).removeStyleName("hidden");
            }
        }
    }

    @Override
    public void show() {

    }

    @UiHandler("employeeName")
    void onClick(ClickEvent e) {

    }
}